package com.ccb.ic.bbs.controller;

import com.ccb.ic.query.bbs.NotificationDTO;
import com.ccb.ic.query.bbs.ResultDTO;
import com.ccb.ic.bbs.enums.NotificationTypeEnum;
import com.ccb.ic.bbs.exception.CustomizeErrorCode;
import com.ccb.ic.bbs.exception.CustomizeException;
import com.ccb.ic.bbs.provider.UserProvider;
import com.ccb.ic.domain.bbs.User;
import com.ccb.ic.bbs.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class NotificationController {

    @Autowired
    private NotificationService notificationService;
    @Autowired
    UserProvider userProvider;

    @GetMapping("/bbs/notification/{id}")
    public Object profile(HttpServletRequest request,
                          @PathVariable(name = "id") Long id) {

        User user = userProvider.getUser("token");
        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.NO_LOGIN);
        }
        NotificationDTO notificationDTO = notificationService.read(id, user);

        if (NotificationTypeEnum.REPLY_COMMENT.getType() == notificationDTO.getType()
                || NotificationTypeEnum.REPLY_SUB_COMMENT.getType() == notificationDTO.getType()
                || NotificationTypeEnum.REPLY_QUESTION.getType() == notificationDTO.getType()
                || NotificationTypeEnum.LIKE_COMMENT.getType() == notificationDTO.getType()
                || NotificationTypeEnum.LIKE_QUESTION.getType() == notificationDTO.getType()) {
            return ResultDTO.okOf(notificationDTO);
        } else {
            return ResultDTO.errorOf("类型不匹配");
        }
    }
}

